import DialogStore from ".";
import { action, observable, computed } from 'mobx';
import { Button, Modal, message } from 'antd';
import {WeaTools,WeaLocaleProvider} from 'ecCom'
import API from "../../apis/formula";
import { SYS_LOCAL_POOLNAME } from "../../constants/dataset";
const getLabel = WeaLocaleProvider.getLabel;

export default class AceEditorDialogStore extends DialogStore {

    @observable dsType = "0";
    @observable dataSource = [];
    @observable selectedDataSource = SYS_LOCAL_POOLNAME;
    @observable dbTables = [];
    @observable quickSearchValue = '';
    @observable tableLoading = false;
    @observable heihgt = 0;
    @observable formulaVisible = false;
    @observable formulaList;
    @observable currentFormulaTitle;
    @observable currentFormulaContent;
    @observable currentFormulaDescribe;
    @observable currentId;
    @observable currentPageId;
    constructor(props) {
        super();
        this.style = {width:620};
        this.title = getLabel(502403,"自定义公式");
        this.buttons = [<Button type="primary" onClick={this.save}>{getLabel(30986,'保存')}</Button>, 
        <Button onClick={this.hide} type="primary">{getLabel(31129,'取消')}</Button>]
    }

    @action openAceEditor = (currentFormula) => {
        if(!this.currentPageId) {
            message.error("请先保存模板，再使用自定义公式");
            return;
        }
        this.formulaVisible = false;
        
        if(currentFormula.id) {
            this.queryById(currentFormula.id);
        } else {
            this.setValue(currentFormula);
        }
        
        this.open();
    }

    @action deleteFormula = (currentFormula) => {
        Modal.confirm({
            title: '删除自定义公式',
            content: '确认删除自定义公式？',
            onOk: () => {
                if(currentFormula.id) {
                    API.deleteFormula({id:currentFormula.id, pageid:this.currentPageId}).then((values) =>{
                        this.formulaList = values;
                        this.loadFormula();
                        message.success(getLabel('502230','删除成功') )
                    });
                }
            }
        });
        
    }

    @action setValue =(currentFormula) => {
        this.currentId ="";
        this.currentFormulaDescribe = "";
        this.currentFormulaTitle = "";
        this.currentFormulaContent = "";
        if(currentFormula) {
            this.currentId = currentFormula.id;
            this.currentFormulaDescribe = currentFormula.describes?currentFormula.describes:"";
            this.currentFormulaTitle = currentFormula.name?currentFormula.name:"";
            this.currentFormulaContent = currentFormula.content?WeaTools.Base64.decode(currentFormula.content):"";
        }
    }

    @action queryById = (id) => {
        API.queryFormulaById(id).then(this.setValue);
    }

    @action save = () => {
        API.saveFormula({
            id:this.currentId,
            name:this.currentFormulaTitle,
            content:WeaTools.Base64.encode(this.currentFormulaContent),
            describes:this.currentFormulaDescribe,
            pageid:this.currentPageId
        }).then((values) =>{
            this.formulaList = values;
            this.loadFormula();
            this.hide();
            message.success(getLabel('502231','保存成功'))
        });
            
    }

    @action showContent =(visible) => {
        this.formulaVisible = visible;
    }

    @action onChange = (value) => {
        this.currentFormulaContent = value;
    }

    @action changeName = (value) => {
        this.currentFormulaTitle = value;
    }

    @action changeDes = (value) => {
        this.currentFormulaDescribe = value;
    }

    @action loadFormula =() => {
        this.parent().loadFormula().forEach(f=>f());
    }

}